As default here::here package uses the directory of project as the root directory, in contrast to the default relative paths that are considered in R. When you run a .Rmd file in a subdirectory of the project, by using the directory of project as the root directory, here::here() function makes life easier if you want to access to other subdirectories of the project. Moreover if you run the script out of the project environment, you might find some difficulties using basic R functions. For example, in .R files, the relative path would change and that would make some troubles. However, here::here function still uses the directory of project as its root directory, since it is looking for .Rproj file.
gapminder_wo_Oceania <- gapminder %>%
filter(continent!="Oceania") %>%
droplevels()
nrow1 <- nrow(gapminder)
nlevels1 <- nlevels(gapminder$continent)
levels1 <- levels(gapminder$continent)
nrow2 <- nrow(gapminder_wo_Oceania)
nlevels2 <- nlevels(gapminder_wo_Oceania$continent)
levels2 <- levels(gapminder_wo_Oceania$continent)
gapminder_wo_Oceania_reordered <- gapminder_wo_Oceania %>%
mutate(continent=fct_reorder(continent,lifeExp,(var)))
levels3=levels(gapminder_wo_Oceania_reordered$continent)
cat(" \n")
cat("**Before:**")
cat(" \n")
gapminder_wo_Oceania %>% arrange(continent) %>% DT::datatable()
cat(" \n")
cat("**After:**")
cat(" \n")
gapminder_wo_Oceania_reordered %>% arrange(continent) %>% DT::datatable()
cat(" <div style= \"float: left;width: 50%; \"> ")
gapminder_wo_Oceania %>%
ggplot() +
geom_bar(aes(continent)) +
coord_flip()+
theme_bw() +
xlab("Continent")+ylab("Number of entries") +
ggtitle("Before")
cat(" </div> ")
cat(" <div style= \"float: right;width: 50%; \"> ")
gapminder_wo_Oceania %>%
ggplot() +
geom_bar(aes(fct_reorder(continent, lifeExp, var))) +
coord_flip()+
theme_bw() +
xlab("Continent")+ylab("Number of entries") +
ggtitle("After")
cat(" </div> ")
cat(" <div style= \"clear:both; \"></div> ")
I have filtered out the rows with Oceania as their continent from the gapminder dataset and droped the respective level. The number of rows, the number of levels, and the levels before and after are shown bellow:
Before:
number of rows= 1704
number of levels= 5
levels: Africa, Americas, Asia, Europe, Oceania
After:
number of rows= 1680
number of levels= 4
levels: Africa, Americas, Asia, Europe
Then I reordered the continent variable by the variance of life expectancy. After that I arranged the datasets and showed the effect of reordering on arranging the datasets by the continent variable. It can be observed that after reordering, arrange() function arranges the dataset based on the new order. Furthermore, the plots number of entries for each continent before and after of the reordering are shown.
The arrangement of levels after reordering is as follows:
levels: Europe, Africa, Americas, Asia
Before: After:gapminder_wo_Oceania <- gapminder %>%
filter(continent!="Oceania") %>%
droplevels()
nrow1 <- nrow(gapminder)
nlevels1 <- nlevels(gapminder$continent)
levels1 <- levels(gapminder$continent)
nrow2 <- nrow(gapminder_wo_Oceania)
nlevels2 <- nlevels(gapminder_wo_Oceania$continent)
levels2 <- levels(gapminder_wo_Oceania$continent)
gapminder_wo_Oceania_reordered <- gapminder_wo_Oceania %>%
mutate(continent=fct_reorder(continent,lifeExp,(var)))
levels3=levels(gapminder_wo_Oceania_reordered$continent)
In this exercise, I used the gapminder data to show the difference between the minimum and maximum of gdp per capita for each continent for year 2007 and save it into variable gdpRange. After writing into csv file and reading it back, the class of variable continent as shown bellow has changed to “character”. I changed it to factor and then reordered it by gdpRange.
After reading back the file:
| continent | gdpPercapRange |
|---|---|
| Africa | 12928.933 |
| Americas | 41750.016 |
| Asia | 46362.990 |
| Europe | 43420.161 |
| Oceania | 9250.358 |
The class of continent variable after reading back the file is: character
After changing the class of continent to factor and reordering it by the gdpRange:
| continent | gdpPercapRange |
|---|---|
| Oceania | 9250.358 |
| Africa | 12928.933 |
| Americas | 41750.016 |
| Europe | 43420.161 |
| Asia | 46362.990 |
I take a plot from homework 3. The minimum and maximum of GDP per capita for all continents are shown in this graph. The old plot is shown on the left side. I tried to make it simpler by removing the x-axis and y-axis labels and adding a title. The new graph on the right side uses the plotly package to make the graph interactive.
cat(" <div style= \"float: left;width: 50%; \"> ")
gapminder %>%
group_by(year,continent) %>%
filter(gdpPercap %in% c(max(gdpPercap),min(gdpPercap))) %>%
mutate(type=ifelse(gdpPercap==min(gdpPercap),"minimum","maximum")) %>%
ggplot()+
geom_line(aes(year,gdpPercap,color=continent,linetype=type))+
scale_y_continuous(name="GDP per capita")+
theme_bw()
cat(" </div> ")
cat(" <div style= \"float: right;width: 50%; \"> ")
oldplot <- gapminder %>%
group_by(year,continent) %>%
filter(gdpPercap %in% c(max(gdpPercap),min(gdpPercap))) %>%
mutate(type=ifelse(gdpPercap==min(gdpPercap),"minimum","maximum")) %>%
ggplot()+
geom_line(aes(year,gdpPercap,color=continent,linetype=type))+
scale_y_continuous(name=" ")+
scale_x_continuous(name=" ")+
ggtitle("Range of gdp per capita ($) since 1950s")+
theme_bw()+
theme(legend.title = element_blank())
oldplot %>% plotly::ggplotly() %>%
plotly::layout(autosize = F, width = 515.625, height = 343.75)
cat(" </div> ")
cat(" <div style= \"clear:both; \"></div> ")
Here you can see the plot embeded in this HTML:
p <- gapminder %>%
group_by(year,continent) %>%
filter(gdpPercap %in% c(max(gdpPercap),min(gdpPercap))) %>%
mutate(type=ifelse(gdpPercap==min(gdpPercap),"minimum","maximum")) %>%
ggplot()+
geom_line(aes(year,gdpPercap,color=continent,linetype=type))+
scale_y_continuous(name="GDP per capita")+
theme_bw()
figname <- "p.png"
ggsave(here::here("hw05",figname),plot=p,width=7.5,height=5,units = "in")